package codeTop;

import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Stack;

public class q239 {
    public int[] maxSlidingWindow(int[] nums, int k) {
        Deque<Integer> queue = new LinkedList<>();
        int[] res = new int[nums.length-k+1];
        for (int i = 0; i < nums.length ; i++) {
            while (!queue.isEmpty()&&queue.getLast()<nums[i]){
                queue.removeLast();
            }
            queue.add(nums[i]);
            if (i - k >= 0&&queue.getFirst()==nums[i-k]){
                queue.removeFirst();
            }
            if (i - k + 1 >= 0) {

                res[i-k+1] = queue.getFirst();
            }
        }
        return res;
    }

    public static void main(String[] args) {
        q239 q = new q239();
        System.out.println(Arrays.toString(q.maxSlidingWindow(new int[]{9, 10, 9, -7, -4, -8, 2, -6}, 5)));
    }
}
